using System;

namespace Ditty.CSharp.ResharperKeygen
{
    public class KeyGen5
    {
        // Fields       
        private const string PRIVATE_KEY = "3483968730802868401158985191409366916534934371594468194468";
        private const string PUBLIC_KEY = "3483968730802868401158985191529641621586542542912639916793";

        // Methods
        private static byte[] GetBytesFromString(string s)
        {
            var buffer = new byte[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                var ch = s[i];
                int num2 = ch;
                buffer[i] = (byte)(num2 & 0x7f);
            }
            return buffer;
        }

        public string GetLicense()
        {
            var num = UserHash();
            var integer = new BigInteger(0xffffL);
            var integer2 = new BigInteger(Version);// (0x1388L);
            var integer3 = new BigInteger(GetBytesFromString(UserName));
            integer = (integer << 0x38) + new BigInteger(num);
            integer2 = integer2 << 0x48;
            integer += integer2;
            var n = new BigInteger(PUBLIC_KEY, 10);
            var modulus = new BigInteger(PRIVATE_KEY, 10);
            var exp = (integer3 | 1).modInverse(modulus);
            return Convert.ToBase64String(integer.modPow(exp, n).getBytes());
        }

        private int UserHash()
        {
            int num;
            var num2 = 0;
            for (num = 0; num < UserName.Length; num++)
            {
                num2 = ((num2 << 7) + UserName[num]) % 0xfff1;
            }
            for (num = 0; num < CompanyName.Length; num++)
            {
                num2 = ((num2 << 7) + CompanyName[num]) % 0xfff1;
            }
            return num2;
        }

        // Properties
        public string UserName { get; set; }
        public string CompanyName { get; set;}
        public int Version { get; set; }
    }
}
